

<template>
  <view v-if="fullItemObj.actualTotal" class="Mall4j">
    <!-- 确认订单 -->
    <!--地址栏 -->
    <view class="address-box" @tap="toAddrListPage">
      <!-- 地址图标 -->
      <view class="addr-icon">
        <image src="https://mall-1312577323.cos.ap-chengdu.myqcloud.com/mall/images/icon/addr.png" />
      </view>
      <!-- 地址为空 -->
      <view v-if="!fullItemObj.userAddr" class="add-addr">{{ i18n.addShippingAddress }}</view>
      <!-- 地址信息 -->
      <block v-if="fullItemObj.userAddr">
        <view class="recipient clearfix">
          <text class="username">{{ fullItemObj.userAddr.receiver }}</text>
          <text class="cellphone">{{ fullItemObj.userAddr.mobile }}</text>
        </view>
        <view class="addr-text">{{ i18n.shippingAddress }}：{{ fullItemObj.userAddr.province }}{{ fullItemObj.userAddr.city }}{{ fullItemObj.userAddr.area }}{{ fullItemObj.userAddr.addr }}</view>
      </block>

      <!-- 右箭头 -->
      <view class="more-icon">
        <image src="https://mall-1312577323.cos.ap-chengdu.myqcloud.com/mall/images/icon/more.png" />
      </view>
      <!-- 下虚线 -->
      <view class="dotted-line">
        <image src="https://mall-1312577323.cos.ap-chengdu.myqcloud.com/mall/images/icon/dotted-line.png" />
      </view>
    </view>
    <!--地址栏end -->

    <!-- 商品信息 -->
    <view class="goods-box">
      <!-- 商品信息 -->
      <view class="goods-msg">
        <view class="goods-img">
          <image :src="fullItemObj.shopCartItem.pic" />
          <view class="miaosha">
            <image src="https://mall-1312577323.cos.ap-chengdu.myqcloud.com/mall/images/icon/miaosha.png" />
          </view>
        </view>
        <view class="goods-text clearfix">
          <view class="goods-name">{{ fullItemObj.shopCartItem.prodName }}</view>
          <text class="goods-price">￥{{ wxs.toPrice(fullItemObj.shopCartItem.seckillPrice) }}</text>
          <text class="goods-price-del">￥{{ wxs.toPrice(fullItemObj.shopCartItem.price) }}</text>
          <text class="goods-amount">x {{ fullItemObj.shopCartItem.prodCount }}</text>
        </view>
      </view>
    </view>
    <!-- 商品信息end -->

    <!-- 配送&留言 -->
    <view class="row">
      <view class="live-message">
        <text class="message-tit">{{ i18n.buyerMessage }}</text>
        <input class="message-int" maxlength="100" :placeholder="i18n.buyerTips" :value="remarks" @input="onRemarksInput"></input>
      </view>
      <view class="item invoice">
        <view class="tit">{{ i18n.invoice.onvoiceIssuance }}：</view>
        <view v-if="!invoiceDataFrom.invoiceType || invoiceDataFrom.invoiceType === 2" class="item" @tap="showInvoicePopup(fullItemObj.shopId)">
          <view class="text-arrow">
            <view class="text">{{ i18n.invoice.noInvoice }}</view>
          </view>
        </view>
        <view v-else class="item" @tap="showInvoicePopup(fullItemObj.shopId,invoiceDataFrom)">
          <view class="text-arrow">
            <!-- <view class="text">本次不开具发票</view> -->
            <view class="text">{{ i18n.invoice.electronic }}({{ i18n.invoice.productDetails }}-{{ invoiceDataFrom.headerName || i18n.invoice.personal }})</view>
          </view>
        </view>
      </view>
    </view>
    <!-- 配送&留言 end -->

    <!-- 商品总额 -->
    <view class="sum">
      <view class="cost-box">
        <view class="goods-table clearfix">
          <text class="goods-table-tit">{{ i18n.goodsAmount }}</text>
          <text class="goods-table-way">￥{{ wxs.toPrice(goodsPrice) }}</text>
        </view>
        <view class="goods-table clearfix">
          <text class="goods-table-tit">{{ i18n.shipping }}</text>
          <text class="goods-table-way">+ ￥{{ wxs.toPrice(transfee) }}</text>
        </view>
        <view v-if="fullItemObj.seckillReduce > 0" class="goods-table clearfix">
          <text class="goods-table-tit">{{ i18n.seckillReduce }}</text>
          <text class="goods-table-way">- ￥{{ wxs.toPrice(fullItemObj.seckillReduce) }}</text>
        </view>
      </view>
      <!-- 合计 -->
      <view class="total-cost">{{ i18n.total }}：<text class="total-num">￥{{ wxs.toPrice(totalPrice) }}</text>
      </view>
    </view>
    <!-- 商品金额 end -->

    <!-- 底部 -->
    <view class="foot-box">
      <view class="foot">
        <view class="total-price">{{ i18n.total }}：<text class="total-price-num">￥{{ wxs.toPrice(totalPrice) }}</text></view>
        <!-- 提交按钮 -->
        <view class="submit-btn" @tap="submitOrder">{{ i18n.submitOrders }}</view>
      </view>
    </view>
    <!-- 底部end -->
    <invoiceEdit v-if="isShowInvoicePopup" :invoice-data-from="invoiceDataFrom" :shop-id="invoiceShopId" :invoice-id="invoiceId" @closePopup="closePopup" @getInvoiceData="getInvoiceData" />
  </view>
</template>

<script module="wxs" lang="wxs" src="../../../wxs/number.wxs"></script>

<script>
// pages/confirm-order/confirm-order.js
var http = require("../../../utils/http.js");
var config = require("../../../utils/config.js");
import {
  PayType,
  AppType
} from "../../../utils/constant.js";
import Pay from "../../../utils/pay.js";
import invoiceEdit from '../../../components/invoice-edit/index.vue'
export default {
  data () {
    return {
      orderPath: '',
      fullItemObj: {},
      totalPrice: 0,
      goodsPrice: 0,
      transfee: 0,
      userAddr: null,
      remarks: "",
      seckillOrderNumber: '',
      pollingTimes: 0,
      seckillOrderStsTimer: '',

      invoiceId: '', // 发票id
      invoiceShopId: '',
      invoiceDataFrom: {},
      isShowInvoicePopup: false
    };
  },

  components: { invoiceEdit },
  props: {},

  computed: {
    i18n () {
      return this.$t('index')
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getSeckill()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //头部导航标题
    uni.setNavigationBarTitle({
      title: this.i18n.makeSureOrder
    });

    var pages = getCurrentPages(); //当前页面栈
    var currPage = pages[pages.length - 1]; //当前页面
    if (currPage.selAddress == "yes") {
      this.setData({
        //将携带的参数赋值
        userAddr: currPage.item
      });
    }
    this.getSeckill();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
  methods: {
    // 发票信息弹窗
    showInvoicePopup (shopId, invoiceDataFrom) {
      if (invoiceDataFrom) {
        this.invoiceDataFrom = invoiceDataFrom
      }
      this.invoiceShopId = shopId
      this.isShowInvoicePopup = true
    },
    getInvoiceData (data) {
      // invoiceDataFrom
      this.invoiceDataFrom = data
      console.log(data)
    },
    closePopup: function () {
      this.setData({
        isShowInvoicePopup: false
      });
    },
    onRemarksInput: function (e) {
      this.setData({
        remarks: e.detail.value
      });
    },

    // 获取orderPath
    getSeckill () {
      http.request({
        url: '/p/seckill/orderPath',
        method: 'GET',
        callBack: res => {
          this.setData({
            orderPath: res
          });
          this.confirmOrder();
        }
      });
    },

    // 确认订单
    confirmOrder () {
      var secKillObj = wx.getStorageSync("secKillObj");
      if (this.userAddr) {
        secKillObj.addrId = this.userAddr.addrId;
      }
      http.request({
        url: `/p/seckill/${this.orderPath}/confirm`,
        method: 'POST',
        data: secKillObj,
        callBack: res => {
          this.setData({
            fullItemObj: res,
            goodsPrice: res.shopCartItem.prodCount * res.shopCartItem.seckillPrice,
            transfee: res.transfee,
            totalPrice: res.transfee + res.shopCartItem.prodCount * res.shopCartItem.seckillPrice,
            userAddr: res.userAddr
          });
        }
      });
    },

    /**
     * 提交订单
     */
    submitOrder () {
      let orderInvoiceDTO = null // invoiceDataFrom
      if (this.invoiceDataFrom && this.invoiceDataFrom.invoiceType === 1) {
        orderInvoiceDTO = this.invoiceDataFrom
      }
      let orderShopParam = {
        remarks: this.remarks,
        shopId: this.fullItemObj.shopCartItem.shopId,
        orderInvoice: orderInvoiceDTO
      }
      let params = {
        url: `/p/seckill/${this.orderPath}/submit`,
        method: 'POST',
        data: orderShopParam,
        selfLoading: true,
        callBack: res => {
          this.seckillOrderNumber = res.orderNumbers
          uni.showLoading({
            title: this.i18n.desperatelyBuying,
            mask: true
          })
          setTimeout(() => {
            this.checkSeckillOrderSubmitStatus()
          }, 3000)
        }
      }
      http.request(params)
    },

    /**
     * 检查秒杀订单提交状态
     */
    checkSeckillOrderSubmitStatus () {
      if (this.pollingTimes < 10) {
        const params = {
          url: '/p/seckill/createOrderStatus',
          method: 'GET',
          data: {
            orderNumber: this.seckillOrderNumber
          },
          callBack: res => {
            if (res) {
              uni.hideLoading()
              clearTimeout(this.seckillOrderStsTimer)
              uni.navigateTo({
                url: '/pages/pay-way/pay-way?orderNumbers=' + this.seckillOrderNumber
              });
            } else {
              this.pollingTimes++
              this.seckillOrderStsTimer = setTimeout(this.checkSeckillOrderSubmitStatus, 3000)
            }
          }
        }
        http.request(params)
      } else {
        uni.hideLoading()
        clearTimeout(this.seckillOrderStsTimer)
        uni.showToast({
          title: this.i18n.secFailTips,
          mask: true,
          icon: 'none'
        })
      }
    },

    /**
     * 选择地址
     */
    toAddrListPage: function () {
      uni.navigateTo({
        url: '/pages/delivery-address/delivery-address?order=0'
      });
    }
  }
};
</script>
<style scoped>
@import "./confirm-order.css";
</style>
